package com.nema.util;

import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.StatementResult;
import software.lib.util.NeoDriver;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * @author sunfiyes 2016-12-9
 * 目标图
 */
public class TargetGraph {
	public double alpha = 0.5;
	
	Map<String, Node> nodeMap = new HashMap<String, Node>();
	Map<String, Map<String, Double>> neighborVec = new HashMap<String, Map<String, Double>>();
	
	public Map<String, Node> createNode(){
		this.nodeMap = new HashMap<String, Node>();
		StatementResult result;
		Record record;

		try(NeoDriver neodriver = new NeoDriver()) {
			result = neodriver.query( "MATCH (n) return  ID(n) As id, Labels(n) As type, n.name As name");
			while ( result.hasNext() )
			{
				record = result.next();
				String id = record.get("id").toString();
				String type = record.get("type").get(0).asString();
				String label = record.get("name").asString();
				this.nodeMap.put(id, new Node(id, type, label));
				//System.out.println(id+","+value);
			}
		}

		return nodeMap;
	}
	
	public Map<String, Map<String, Double>> createNeighborVec(){
		neighborVec = new HashMap<String, Map<String, Double>>();
		Map<String, Double> vec;

		try(NeoDriver neodriver = new NeoDriver()) {
			Iterator i = nodeMap.entrySet().iterator();
			while (i.hasNext()) {
				Map.Entry entry = (Map.Entry) i.next();
				String v = (String) entry.getKey();
				vec = new HashMap<String, Double>();
				StatementResult result = neodriver.query("start n=node(" + v + ")  MATCH (n)-[]-(x) RETURN ID(x) As id");
				while (result.hasNext()) {
					Record record = result.next();
					if (nodeMap.containsKey(record.get("id").toString()))
						vec.put(record.get("id").toString(), alpha);
				}
				neighborVec.put(v, vec);
			}
		}
		return neighborVec;
	}
	
}
